package com.chyb.cloud.service.Impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.chyb.cloud.entities.Storage;
import com.chyb.cloud.mapper.StorageMapper;
import com.chyb.cloud.service.StorageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class StorageServiceImpl implements StorageService {

    @Autowired
    private StorageMapper storageMapper;

    @Override
    public void decrease(Long productId, Integer count) {

        log.info("------->storage-service中扣减库存开始");
        LambdaUpdateWrapper<Storage> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper
                /*条件*/
                .eq(Storage::getProductId, productId)
                /*字段自增*/
                .setIncrBy(Storage::getUsed, count)
                /*字段自减*/
                .setDecrBy(Storage::getResidue, count);
        int update = storageMapper.update(updateWrapper);

        log.info("------->storage-service中扣减库存结束");
    }
}
